﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DLog.Tools
{
    public class NotepadHelper
    {
        /// <summary>
        /// 创建文件夹
        /// </summary>
        public static void CreateLogFile(string filePath)
        {
            string directoryPath = filePath.Substring(0, filePath.LastIndexOf('\\'));
            if (!Directory.Exists(directoryPath))
            {
                try
                {
                    Directory.CreateDirectory(directoryPath);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            if (!File.Exists(filePath))
            {
                try
                {
                    FileStream file = File.Create(filePath);
                    file.Dispose();
                    file.Close();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
        /// <summary>
        ///向日志文件中写入日志
        /// </summary>
        /// <param name="logText"></param>
        public static void LogWrite(string logText, string filePath)
        {
            CreateLogFile(filePath);
            StreamWriter sw = new StreamWriter(filePath, true, System.Text.Encoding.UTF8);
            try
            {
                sw.WriteLine(logText);
                sw.Flush();
            }
            finally
            {
                sw.Dispose();
                sw.Close();
            }
        }
        /// <summary>
        ///向日志文件中写入日志
        /// </summary>
        /// <param name="logText"></param>
        public static void LogWrite(Dictionary<string, string> log, string filePath)
        {
            LogWrite(log, ':', filePath);
        }
        /// <summary>
        ///向日志文件中写入日志
        /// </summary>
        /// <param name="logText"></param>
        public static void LogWrite(Dictionary<string, string> log, char splitStr, string filePath)
        {
            CreateLogFile(filePath);
            StreamWriter sw = new StreamWriter(filePath, true, System.Text.Encoding.UTF8);
            try
            {
                for (int i = 0; i < log.Count; i++)
                {
                    sw.WriteLine(log.ElementAt(i).Key + splitStr + log.ElementAt(i).Value);
                }
                sw.Flush();
            }
            finally
            {
                sw.Dispose();
                sw.Close();
            }
        }
        /// <summary>
        /// 向日志文件中写入日志
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="filePath"></param>
        public static void LogWrite(string logKey, string logValue, string filePath)
        {
            LogWrite(logKey, ':', logValue, filePath);
        }
        /// <summary>
        /// 向日志文件中写入日志
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="filePath"></param>
        public static void LogWrite(string logKey, char splitStr, string logValue, string filePath)
        {
            CreateLogFile(filePath);
            StreamWriter sw = new StreamWriter(filePath, true, System.Text.Encoding.UTF8);
            try
            {
                sw.WriteLine(logKey + splitStr + logValue);
                sw.Flush();
            }
            finally
            {
                sw.Dispose();
                sw.Close();
            }
        }
        /// <summary>
        /// 向日志文件中写入日志
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="filePath"></param>
        public static void LogWrite(Exception ex, string filePath)
        {
            CreateLogFile(filePath);
            StreamWriter sw = new StreamWriter(filePath, true, System.Text.Encoding.UTF8);
            try
            {
                sw.WriteLine("/********************************/");
                sw.WriteLine("日期：" + System.DateTime.Now.ToString());
                sw.WriteLine();
                sw.WriteLine("错误源：" + ex.Source);
                sw.WriteLine("错误信息：" + ex.Message);
                sw.WriteLine("/********************************/");
                sw.WriteLine();
                sw.WriteLine();
                sw.Flush();
            }
            finally
            {
                sw.Dispose();
                sw.Close();
            }
        }
    }
}
